/*
	This file is part of the JKTest
    Copyright (C) 2008 Royce Y. Feng <ryfeng@gmail.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License version 3
    as published by the Free Software Foundation. You may not use, modify
    or distribute this program under any other version of the
    GNU Affero General Public License.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package JKPanels;

import java.awt.Dimension;
import java.io.IOException;
import javax.swing.JOptionPane;
import javax.swing.BoxLayout;
import javax.swing.JPanel;

import jktest.JKMainPanel;
import jktest.JKMainGui;
import jktest.JMultilineLabel;
import jktest.JKButton;

/**
 * 
 * Panel Class containing all elements of the Device Test
 * 
 * @author Royce Feng
 */

public class DevicePanel extends JKMainPanel {

    private PassFailPanel passFail;
    
    /**
     * 
     * Main Constructor for DevicePanel. Creates a multiline label with instructions
     * as well as the three main buttons: Start test, Pass, Fail.
     * 
     * @param _parent The Parent Class (for communication)
     */
    public DevicePanel ( JKMainGui _parent ) {
       super(_parent);
       JMultilineLabel instructions = new JMultilineLabel();
       instructions.setMaxWidth(500);
       instructions.setText("After you click the button, make sure that none of the devices" +
               "have exclamation points or question marks on them. If they do have those things either install" +
               "or reinstall the driver");
       this.add(instructions);
       JKButton button = new JKButton(this,"Device Manager");
       button.setPreferredSize(new Dimension(200,25));
       this.add( button );
       passFail = new PassFailPanel(this);
       passFail.setPreferredSize(new Dimension(500,25));
       this.add(passFail);
    }
    
    /**
     * 
     * Returns the name of the panel
     * 
     * @return The name of the panel
     */
    @Override
    public String getName() {
        return "Devices";
    }
    
    /**
     * 
     * Handles button clicks. Pass/fail gets sent to parent while the test is run
     * when the test button is clicked.
     * 
     * @param source Button source (usually the button text)
     */
    public void ButtonClicked( String source ) {
        if ( source.equals("Pass") ) {
            getThisParent().valChanged(this.getName(), true);
            getThisParent().incPanel();
        } else if ( source.equals("Fail") ) {
            getThisParent().valChanged(this.getName(), false);
            getThisParent().incPanel();
        } else {
            String osName = System.getProperty("os.name");
            if (osName.toUpperCase().startsWith("WINDOWS")) {
                try {
                    String root = System.getenv("SystemRoot");
                    String ps = root + "\\system32\\mmc.exe /s " + root + "\\system32\\devmgmt.msc";
                    Runtime.getRuntime().exec(ps);
                } catch ( IOException e ) {
                    JOptionPane.showMessageDialog(this, "Device Manager could not be accessed", "Program Error", JOptionPane.ERROR_MESSAGE);
                    getThisParent().valChanged(this.getName(), false);
                }
            } else {
                JOptionPane.showMessageDialog(this,"Current Operating System is not windows","Unrecognized Operating System", JOptionPane.ERROR_MESSAGE);
            }
        }
    }
    
}
